package cn.spring.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.spring.bean.Commodity;
import cn.spring.bean.Employee;
import cn.spring.bean.Shoppingcart;
import cn.spring.bean.Today;
import cn.spring.dao.CommodityMapper;
import cn.spring.dao.OrdersMapper;
import cn.spring.dao.ShoppingcartMapper;
import cn.spring.service.ShoppingcartService;

@Service
@Transactional
public class ShoppingcartServiceImpl implements ShoppingcartService {
	@Resource
	private ShoppingcartMapper dao;
	
	@Resource
	private OrdersMapper oDao;
	
	@Resource
	private CommodityMapper cDao;

	@Override
	public Boolean save(String id,String num,Employee emp,int ordId) {
		// TODO Auto-generated method stub
		String[] ids=id.split("-");
		String[] nums=num.split("-");
		int count=0;
		Boolean pan=false;
		for(String str:ids){
			int shu=Integer.parseInt(nums[count]);
			if(shu>0) {
				Shoppingcart cart=new Shoppingcart();
				Commodity com=cDao.selectByPrimaryKey(Integer.parseInt(str));
				cart.setCommodityId(Integer.parseInt(str));
				cart.setIsGou(false);
				cart.setOrdId(ordId);
				cart.setNum(shu);
				cart.setEmpId(emp.getId());
				Double total=com.getPrice()*Double.parseDouble(nums[count]);
				cart.setTotal(total);
				int pan1=dao.insert(cart);
				if(pan1>0){
					pan=true;
				}else{
					pan=false;
					break;
				}
			}else {
				pan=true;
			}
			count++;
		}
		return pan;
	}

	@Override
	public List<Map<String, Object>> selectIsGou(int empId) {
		// TODO Auto-generated method stub
		return dao.selectIsGou(empId);
	}

	@Override
	public int updateIsGou(int empId,int ordId) {
		// TODO Auto-generated method stub
		int count=oDao.updateState(3, ordId);
		if(count>0){
			count=dao.updateIsGou(empId,ordId);
			return count;
		}else{
			return 0;
		}
	}

	@Override
	public List<Map<String, Object>> selectByState(int empId,int ordId) {
		// TODO Auto-generated method stub
		return dao.selectByState(empId,ordId);
	}

	@Override
	public List<Map<String, Object>> selectByState1(int ordId) {
		// TODO Auto-generated method stub
		return dao.selectByState1(ordId);
	}

	@Override
	public Map<Today, List<Map<String, Object>>> today() {
		// TODO Auto-generated method stub
		List<Today> obj=dao.toDay();
		Map<Today, List<Map<String, Object>>> list=new LinkedHashMap<Today, List<Map<String, Object>>>();
		for(Today ob:obj){
			List<Map<String, Object>> cList=dao.selectByState1(ob.getOrdid());
			list.put(ob, cList);
		}
		return list;
	}

	@Override
	public int queren(int ordId) {
		// TODO Auto-generated method stub
		return oDao.updateState(4, ordId);
	}

	@Override
	public Double getTotal(String startTime,String endTime) {
		// TODO Auto-generated method stub
		return dao.getTotal(startTime,endTime);
	}

	@Override
	public List<Map<String, Object>> toDayCommodity() {
		// TODO Auto-generated method stub
		return dao.toDayCommodity();
	}

	@Override
	public List<Map<String, Object>> toDayCommodityByWord(String word) {
		// TODO Auto-generated method stub
		word="%"+word+"%";
		return dao.toDayCommodityByWord(word);
	}

	@Override
	public Map<Today, List<Map<String, Object>>> toDayByKey(String word) {
		// TODO Auto-generated method stub
				List<Today> obj=dao.toDayByKey(word);
				Map<Today, List<Map<String, Object>>> list=new LinkedHashMap<Today, List<Map<String, Object>>>();
				for(Today ob:obj){
					List<Map<String, Object>> cList=dao.selectByState1(ob.getOrdid());
					list.put(ob, cList);
				}
				return list;
	}

	@Override
	public Double getTotalByKey(String startTime, String endTime, String word) {
		// TODO Auto-generated method stub
		word="%"+word+"%";
		return dao.getTotalByKey(startTime, endTime, word);
	}
	
	@Override
	public List<Map<String, Object>> getShang(int ordId) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> cList=dao.selectByState1(ordId);
		return cList;
	}

	@Override
	public List<Today> today1() {
		// TODO Auto-generated method stub
		List<Today> obj=dao.toDay();
		return obj;
	}

	@Override
	public List<Map<String, Object>> findAccounts() {
		// TODO Auto-generated method stub
		return dao.findAccounts();
	}

	@Override
	public List<Map<String, Object>> finGoodsByOId(int oid) {
		// TODO Auto-generated method stub
		return dao.finGoodsByOId(oid);
	}
	

	
}
